package com.qf.web;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Employee;
import com.qf.service.EmployeeService;
import com.qf.util.enum1.PageEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * 2020/2/2
 * 键盘敲烂，月薪过万
 * 登高必自卑，行远必自迩。
 * 不忘初心：像初学者一样拥有一颗谦虚、开放的心态。
 * 李学峰
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //用户查询模块 index表示页码值
    @RequestMapping("/resource/demo2/getusers")
    public String getall(
            @RequestParam(defaultValue = "1") int index, ModelMap map,
            String username,String rolename,String deptname) {
        PageInfo<Employee> getall = employeeService.getall(index, PageEnum.pagesize.getSize(),username,rolename,deptname);
        map.addAttribute("pi", getall);
        //存储模糊条件到页面
        map.addAttribute("uname",username);
        map.addAttribute("rname",rolename);
        map.addAttribute("dname",deptname);
        return "/resource/demo2/list";
    }

    //退出
    @RequestMapping("/loginout")
    public String loginout(HttpSession session) {
        //清楚seesion数据
        session.invalidate();
        return "redirect:/login.jsp";
    }

    //登录
    @RequestMapping("/login")
//这个是前端往后台传值 必须和前端的name的定义名一样
    public String login(String username, String password
            , HttpSession session) {
        Employee login = employeeService.login(username, password);
        if (login == null) {
            return "login";
        }
        //成功就把信息保存在session中

        session.setAttribute("user", login);
        return "redirect:/index.jsp";
//    redirect重定向
    }

}
